package com.lijie.ljblog.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lijie.ljblog.model.Mail;
import com.lijie.ljblog.model.Person;
import com.lijie.ljblog.model.dto.PersonDTO;
import com.lijie.ljblog.service.PersonService;
import com.lijie.ljblog.service.UtilService;
import com.lijie.ljblog.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijie
 * @since 2021-08-28
 */
@RestController
@RequestMapping("ljblog/person/")
public class PersonController {
    @Autowired
    private PersonService personService;

    @Autowired
    UtilService utilService;


    @Value("${fileUploadUrl}")
    String fileUploadUrl;
    @RequestMapping("login")
    public Result<Person> adminLogin(@RequestBody Person person1){
        Result<Person> result=new Result<>();
        try {
            result= personService.login(person1);
        } catch (Exception e) {
            result.setErrored("登录出错了，请稍后再试");
        }
        return result;
    }
    @RequestMapping("islogin")
    public Person islogin(HttpServletRequest request){
        Person user =(Person) request.getSession().getAttribute("user");
        System.out.println(user);
        return user;
    }
    @RequestMapping("userLogOut")
    public Person userLogOut(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        Person user =(Person) request.getSession().getAttribute("user");
        return user;
    }

    //验证邮箱或者用户名是否被注册
    @RequestMapping("checkPersonUsed")
    public Result<Boolean> checkEmailUsed(@RequestBody Person person){
        Result<Boolean> result=new Result<>();
        try {
            Boolean aBoolean = personService.checkEmailUsed(person);
            if(aBoolean){
                result.setSuccess();
            }else {
                result.setErrored("邮箱已被注册");
            }
        }catch (Exception e){
            result.setErrored("出现异常："+e);
        }
        return result;
    }
    //注册请求验证码,
    @RequestMapping("emailVerification")
    public Result<Integer> emailVerification(@RequestBody Mail mail){
        Result<Integer> result=new Result<>();
        try {
            personService.emailVerification(mail.getEmail());
            result.setSuccess("验证码已经发送至邮箱，请查收。10分钟内有效");
            result.setData(60000);
        } catch (Exception e) {
            result.setData(0);
            result.setErrored(e.getMessage());
        }
        return result;
    }
    //注册请求
    @RequestMapping("userRegister")
    public Result<Boolean> userRegister(@RequestBody Person person){
        Result<Boolean> result=new Result<>();
        try {
            Boolean aBoolean = personService.personRegister(person);
            if(aBoolean){
                result.setSuccess();
            }else{
                result.setErrored("注册失败");
            }
        } catch (Exception e) {
            result.setErrored(e.getMessage());
        }
        return result;
    }
    @RequestMapping("forgetPassword")
    public Result<Boolean> forgetPassword(@RequestBody Person person){
        Result<Boolean> result=new Result<>();
        try{
            if(person!=null){
                Boolean aBoolean = personService.forgetPassword(person);
                if(aBoolean){
                    result.setSuccess("密码重置成功！");
                }else{
                    result.setErrored("密码重置失败");
                }
            }else{
                result.setErrored("参数解析为空");
            }
        }catch (Exception e){
            result.setErrored(e.getMessage());
        }
        return result;
    }

    /**
     * 图片上传
     * @param mulRequest
     * @param request
     * @return
     */
    @PostMapping("headUpload")
    public Result<String> headUpload(MultipartHttpServletRequest mulRequest, HttpServletRequest request){
        Result<String> res=new Result<>();
        try {
            boolean b = personService.headUpload(mulRequest, request);
            res.setSuccess("修改成功");
        } catch (IOException e) {
            res.setSuccess(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 用户头像修改，适用于图片已上传，只修改用户表头像路径
     * @param userDTO
     *
     * @return
     */
    @PostMapping("headUpdate")
    public Result<String> headUpdate(@RequestBody PersonDTO userDTO){
        Result<String> res=new Result<>();
        try {
            UpdateWrapper<Person> updateWrapper=new UpdateWrapper<>();
            String headImg = userDTO.getHeadImg().substring(fileUploadUrl.length());
            updateWrapper.set("headImg", headImg)
                    .eq("id",userDTO.getUserId());
            boolean update = personService.update(updateWrapper);
            if(update){
                res.setSuccess("修改成功");
            }else{
                res.setErrored("修改失败");
            }

        } catch (Exception e) {
            res.setSuccess(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    /**
     *  更具person相关信息查person
     * @param person
     * @return
     */
    @RequestMapping("getUser")
    public Result<Person> getPerson(@RequestBody Person person){
        return personService.getPerson(person);
    }


}

